package com.ddzh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddzh.dto.RoleMenuDto;
import com.ddzh.mapper.RoleMenuMapper;
import com.ddzh.pojo.RoleMenu;
import com.ddzh.service.RoleMenuService;
import org.apache.dubbo.config.annotation.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {
    @Override
    public boolean deleteByRoleId(Long roleId) {
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(roleId);
        roleMenu.setIs_delete(1);
        return this.updateById(roleMenu);
    }

    @Override
    public Long[] findMenuIdsByRoleId(Long roleId) {
        return this.baseMapper.findMenuIdsByRoleId(roleId);
    }


    @Override
    public boolean allocateUserMenu(RoleMenuDto roleMenuDto) {
        //移除 角色旧的用户关系
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleMenuDto.getRoleId());
        this.remove(queryWrapper);

        //新增角色-用户关系
        List<RoleMenu> roleMenuList = new ArrayList<>();
        if (roleMenuDto.getMenuIds().length>0){
            for (Long menuId : roleMenuDto.getMenuIds()) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setMenuId(menuId);
                roleMenu.setRoleId(roleMenuDto.getRoleId());
                roleMenuList.add(roleMenu);
            }
            this.saveBatch(roleMenuList);
        }
        return true;
    }
}
